#include "../../include/config/CConfigIni.h"
#include <string>

std::string CConfigIni::APP_PATH = boost::filesystem::initial_path<boost::filesystem::path>().string();

std::shared_ptr<CConfigIni> CConfigIni::m_pInstance = nullptr;
CLock CConfigIni::m_lock;

CConfigIni::CConfigIni()
    :errMsg("")
	,dbHost("")
	,dbUser("")
	,dbPwd("")
	,dbSchema("")
	,dbPort(2020)
    ,tcpPort(5555)
{}

CConfigIni::~CConfigIni(){}

bool CConfigIni::LoadConfigIni(std::string ini_path)
{
    using namespace boost::property_tree;
    ptree lvptProperties;

    try
    {
        ini_parser::read_ini(ini_path, lvptProperties);             //读取配置文件

        //section:db
        {
            basic_ptree<std::string, std::string> lvbtItems = lvptProperties.get_child("DB");                        
            dbHost = lvbtItems.get<std::string>("dbHost");    
            dbUser = lvbtItems.get<std::string>("dbUser");    
            dbPwd = lvbtItems.get<std::string>("dbPwd");    
            dbSchema = lvbtItems.get<std::string>("dbSchema");      
            dbPort = lvbtItems.get<unsigned int>("dbPort");
        }   

        //section:tcp server
        {
            basic_ptree<std::string, std::string> lvbtItems = lvptProperties.get_child("SERVER");                        
            tcpPort = lvbtItems.get<unsigned int>("serverPort");    
        }                      
    }
    catch (std::exception& e)
    {
        errMsg = e.what();
        return false;
    }

	return true;
}

bool CConfigIni::SaveConfigIni(std::string ini_path)
{
    using namespace boost::property_tree;
    ptree lvptProperties;

    try
    {       
       //lvptProperties.put<unsigned int>("server.server_port", serverPort);       
       //ini_parser::write_ini(ini_path, lvptProperties);      
      // ini_parser::write_ini(ini_path, lvptProperties); 
    }
    catch (std::exception& e)
    {
        errMsg = e.what();
        return false;
    }

    return true;
}